﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KinectServer.Module;

namespace KinectServer
{
    /// <summary>
    /// SubWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class SubWindow : Window
    {
        private Kinect _kinect;

        private Action<String> Log;

        private Action Close;

        public SubWindow()
        {
            InitializeComponent();

            //창 위치 세팅
            this.Top = 10;
            this.Left = SystemParameters.PrimaryScreenWidth - 400;

            //키넥트 데이터 수신준비
            _kinect = ((KinectServer.App)App.Current).KinectObject;
            ((KinectServer.App)App.Current).ServerObject.ServerClosed = this.OnServerClosed;

            KinectServer.Module.Log.Output = this.OnOutput;

            Log = msg =>
            {
                this.txtConsole.Text += String.Format("{0}{1}", msg, Environment.NewLine);
                this.txtConsole.ScrollToEnd();
            };

            Close = () =>
            {
                this.btnStartStopServer.IsChecked = false;
            };

        }

        private void OnServerClosed()
        {
            this.Dispatcher.BeginInvoke(Close);
        }

        private void OnOutput(String message)
        {
            this.Dispatcher.BeginInvoke(Log, message);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            App.Current.MainWindow.Close();
        }

        private void btnCamUp_Click(object sender, RoutedEventArgs e)
        {
            _kinect.CameraUp(5);
        }

        private void btnCamDown_Click(object sender, RoutedEventArgs e)
        {
            _kinect.CameraDown(5);
        }
        
        private void btnCamZero_Click(object sender, RoutedEventArgs e)
        {
            _kinect.CameraZero();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnStartStopServer_Click(object sender, RoutedEventArgs e)
        {
            if (btnStartStopServer.IsChecked == true)
            {
                String ip = txtIP.Text;
                int port = int.Parse(txtPort.Text);

                ((KinectServer.App)App.Current).ServerObject.Start(ip, port);
            }
            else
            {
                ((KinectServer.App)App.Current).ServerObject.Stop();
            }
        }

    }
}
